﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.IO;

namespace welding
{
    public class XMLHelper
    {
        public string ReadXml(string XmlFileName, string item)
        {
            if (File.Exists(XmlFileName) == false) return "";
            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(XmlFileName);
                XmlElement xe = xmlDoc.DocumentElement; // DocumentElement 获取xml文档对象的根XmlElement.

                XmlNodeList xnl = xe.GetElementsByTagName(item);
                if (xnl == null)
                    return null;
                if (xnl.Count == 0)         //没有找到标签
                    return null;
                return xnl.Item(0).InnerText;
            }
            catch (System.Exception ex)
            {
                Console.WriteLine("ReadXml error :" + ex.Message);
                return "";
            }
        }

        //读取多个项目
        public string[] ReadXml(string XmlFileName, string[] items)
        {
            if (File.Exists(XmlFileName) == false) return null;

            string[] returnItems = new string[items.Length];

            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(XmlFileName);
                XmlElement xe = xmlDoc.DocumentElement; // DocumentElement 获取xml文档对象的根XmlElement.

                int idx = 0;
                foreach (string strTemp in items)
                {
                    XmlNodeList xnl = xe.GetElementsByTagName(strTemp);
                    if (xnl == null)
                        return null;
                    if (xnl.Count == 0)         //没有找到标签
                        return null;

                    returnItems[idx] = xnl.Item(0).InnerText;
                    idx++;
                }
            }
            catch (System.Exception ex)
            {
                Console.WriteLine("ReadXml error :" + ex.Message);
                return null;
            }
            return returnItems;
        }


        public void WriteXml(string XmlFileName, string item, string value)
        {
            if (File.Exists(XmlFileName) == false) return;
            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(XmlFileName);
                XmlElement xe = xmlDoc.DocumentElement; // DocumentElement 获取xml文档对象的根XmlElement.
                XmlNodeList xnl = xe.GetElementsByTagName(item);
                if (xnl == null)
                    return;
                if (xnl.Count == 0)         //没有找到标签
                    return;
                xnl.Item(0).InnerText = value;
                xmlDoc.Save(XmlFileName);
            }
            catch (System.Exception ex)
            {
                Console.WriteLine("WriteXml error :" + ex.Message);
            }
        }


        //写入多个项目
        public void WriteXml(string XmlFileName, Dictionary<string,string> items)
        {
            if (File.Exists(XmlFileName) == false) return;

            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(XmlFileName);
                XmlElement xe = xmlDoc.DocumentElement; // DocumentElement 获取xml文档对象的根XmlElement.

                foreach (KeyValuePair<string, string> kvp in items)
                {
                    XmlNodeList xnl = xe.GetElementsByTagName(kvp.Key);
                    if (xnl == null)
                        return;
                    if (xnl.Count == 0)         //没有找到标签
                        return;
                    xnl.Item(0).InnerText = kvp.Value;
                }
                xmlDoc.Save(XmlFileName);
            }
            catch (System.Exception ex)
            {
                Console.WriteLine("WriteXml error :" + ex.Message);
            }
        }
    }
}